package com.simple.service.impl;

import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.simple.entity.SysRole;
import com.simple.entity.vo.SysRoleVo;
import com.simple.repository.SysRoleRepository;
import com.simple.service.SysRoleService;
import com.simple.utils.RedisUtils;
import com.simple.utils.SysConfig;

@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleRepository sysRoleRepository;
    
    @Autowired
    RedisUtils redisUtils;

    @Override
    public SysRole findByRoleId(Integer id) {
        return sysRoleRepository.findByRoleId(id);
    }

    @Override
    public String findMenuByUserName(String username) {
        return sysRoleRepository.findMenuByUserName(username);
    }

    @Override
    public List<SysRole> findAll() {
        return sysRoleRepository.findAll();
    }

    @Transactional
    @Override
    public SysRole saveAndFlush(SysRole sysRole) {
        if(null==(sysRole.getId())) {
        	sysRole.setCreateDate(new Date());
        }
        //删除缓存
        redisUtils.batchDelete(SysConfig.MENU_LIST+"*");
        return sysRoleRepository.saveAndFlush(sysRole);
    }
    @Override
    public void deleteById(Integer id) {
        sysRoleRepository.deleteById(id);
    }

    @Override
    public List<SysRoleVo> findAllRoles() {
        return sysRoleRepository.findAllRoles();
    }
    
}

